<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Websocket Example - Record Plugin for Video.js</title>

  <link href="../../node_modules/video.js/dist/video-js.min.css" rel="stylesheet">
  <link href="../../node_modules/videojs-wavesurfer/dist/css/videojs.wavesurfer.min.css" rel="stylesheet">
  <link href="../../dist/css/videojs.record.css" rel="stylesheet">
  <link href="../assets/css/examples.css" rel="stylesheet">

  <script src="../../node_modules/video.js/dist/video.min.js"></script>
  <script src="../../node_modules/recordrtc/RecordRTC.js"></script>
  <script src="../../node_modules/webrtc-adapter/out/adapter.js"></script>
  <script src="../../node_modules/wavesurfer.js/dist/wavesurfer.min.js"></script>
  <script src="../../node_modules/wavesurfer.js/dist/plugin/wavesurfer.microphone.min.js"></script>
  <script src="../../node_modules/videojs-wavesurfer/dist/videojs.wavesurfer.min.js"></script>

  <script src="../../dist/videojs.record.js"></script>

  <script src="../../node_modules/sockjs-client/dist/sockjs.min.js"></script>

  <script src="../browser-workarounds.js"></script>

  <style>
  /* change player background color */
  #myAudio {
      background-color: #35F291;
  }
  </style>
</head>
<body>

<audio id="myAudio" class="video-js vjs-default-skin"></audio>

<script>
/* eslint-disable */
var sock;
var startedRecord = false;
var options = {
    controls: true,
    bigPlayButton: false,
    width: 320,
    height: 240,
    fluid: false,
    plugins: {
        wavesurfer: {
            backend: 'WebAudio',
            waveColor: '#19613C',
            progressColor: 'black',
            displayMilliseconds: true,
            debug: true,
            cursorWidth: 1,
            hideScrollbar: true,
            plugins: [
                WaveSurfer.microphone.create({
                    bufferSize: 4096,
                    numberOfInputChannels: 1,
                    numberOfOutputChannels: 1,
                    constraints: {
                        video: false,
                        audio: true
                    }
                })
            ]
        },
        record: {
            audio: true,
            video: false,
            maxLength: 10,
            audioBufferUpdate: true,
            displayMilliseconds: true,
            debug: true
        }
    }
};

// apply some workarounds for certain browsers
applyAudioWorkaround();

var player = videojs('myAudio', options, function() {
    // print version information at startup
    var msg = 'Using video.js ' + videojs.VERSION +
        ' with videojs-record ' + videojs.getPluginVersion('record') +
        ', videojs-wavesurfer ' + videojs.getPluginVersion('wavesurfer') +
        ', wavesurfer.js ' + WaveSurfer.VERSION + ' and recordrtc ' + RecordRTC.version;
    videojs.log(msg);

    // connect to websocket server
    var sockjs_url = 'http://0.0.0.0:9999/upload-socket';
    console.log('connecting to websocket: ' + sockjs_url);
    sock = new SockJS(sockjs_url);

    // websocket event handlers
    sock.onopen = function() {
        console.log('connected to websocket: ' + sockjs_url);
    };
    sock.onmessage = function(e) {
        console.log('message received: ', e.data);
    };
    sock.onclose = function() {
        console.log('websocket connection closed.');
    };
});

// error handling
player.on('deviceError', function() {
    console.log('device error:', player.deviceErrorCode);
});

player.on('error', function(element, error) {
    console.error(error);
});

// user clicked the record button and started recording
player.on('startRecord', function() {
    startedRecord = true;
    sock.send('start');
    console.log('started recording!');

    player.on('audioBufferUpdate', function() {
        // upload recorded data
        upload(player.recordedData);
    });
});

// user completed recording and stream is available
player.on('finishRecord', function() {
    startedRecord = false;

    // stop uploading
    player.off('audioBufferUpdate');

    sock.send('stop');

    // the blob object contains the recorded data that
    // can be downloaded by the user, stored on server etc.
    console.log('finished recording:', player.recordedData);
});

function upload(data) {
    sock.send(data);
}
</script>

</body>
</html>
